#################################################################################
# Do-file:		all-figures.R
# Date:			4/5/24
# Author:			Stephanie Karol
# Description:	Produce figures for "Effects of COVID-19 on the Nonprofit Sector"
#################################################################################

library("tidyverse")
library("ggplot2")
library("plyr")
library("dplyr")
library("viridis")
library("haven")

setwd("Replication")

# Figure 1: Variation in Fiscal Year
# this is a tikz picture: use fig1tikz.tex to create it

# Figure 2: Pre-Trends

pretrendmat<-read.csv("pretrendmat.csv",stringsAsFactors=F,header=T)
pretrendmat$year<-c(2014:2018,NA,2014:2018,NA,2014:2018,NA,2014:2018,NA,2014:2018,NA )
pretrendmat$label<-c(rep("Contributions",6),rep("Government Grants",6),rep("Salaries",6),rep("Employment",6),rep("Program Spending",6) )


wrapper <- function(x, ...) paste(strwrap(x, ...), collapse = "\n")

# Figure 2(a)
ggplot(pretrendmat[c(1:5),], aes(year, pretrendmatb))+
  geom_point()+
  geom_line(data=pretrendmat[c(1:5),],aes(color = label))+
  geom_ribbon(data=pretrendmat[c(1:5),],aes(ymin=pretrendmatb-pretrendmatse*1.96,ymax=pretrendmatb+pretrendmatse*1.96,fill=label),alpha=0.3)+
  theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "none",legend.title=element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=20),legend.text = element_blank())+ylim(-0.25,0.5)+geom_hline(yintercept = 0)+
  annotate("label", x=2017.5, y=0.45, label = wrapper(paste0(as.character(round_any(pretrendmat[6,1],0.001)),"  p=",as.character(round_any(pretrendmat[6,2],0.001)),collapse=NULL),width=5), size = 8)
ggsave("figure2a.pdf")

# Figure 2(b)
ggplot(pretrendmat[c(7:11),], aes(year, pretrendmatb))+
  geom_point()+
  geom_line(data=pretrendmat[c(7:11),],aes(color = label))+
  geom_ribbon(data=pretrendmat[c(7:11),],aes(ymin=pretrendmatb-pretrendmatse*1.96,ymax=pretrendmatb+pretrendmatse*1.96,fill=label),alpha=0.3)+
  theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "none",legend.title=element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=20),legend.text = element_blank())+ylim(-0.25,0.5)+geom_hline(yintercept = 0)+
  annotate("label", x=2017.5, y=0.45, label = wrapper(paste0(as.character(round_any(pretrendmat[12,1],0.001)),"  p=",as.character(round_any(pretrendmat[12,2],0.001)),collapse=NULL),width=5), size = 8)
ggsave("figure2b.pdf")

# Figure 2(c)
ggplot(pretrendmat[c(13:17),], aes(year, pretrendmatb))+
  geom_point()+
  geom_line(data=pretrendmat[c(13:17),],aes(color = label))+
  geom_ribbon(data=pretrendmat[c(13:17),],aes(ymin=pretrendmatb-pretrendmatse*1.96,ymax=pretrendmatb+pretrendmatse*1.96,fill=label),alpha=0.3)+
  theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "none",legend.title=element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=20),legend.text = element_blank())+ylim(-0.25,0.5)+geom_hline(yintercept = 0)+
  annotate("label", x=2017.5, y=0.45, label = wrapper(paste0(as.character(round_any(pretrendmat[18,1],0.001)),"  p=",as.character(round_any(pretrendmat[18,2],0.001)),".000",collapse=NULL),width=5), size = 8)
ggsave("figure2c.pdf")

# Figure 2(d)
ggplot(pretrendmat[c(19:23),], aes(year, pretrendmatb))+
  geom_point()+
  geom_line(data=pretrendmat[c(19:23),],aes(color = label))+
  geom_ribbon(data=pretrendmat[c(19:23),],aes(ymin=pretrendmatb-pretrendmatse*1.96,ymax=pretrendmatb+pretrendmatse*1.96,fill=label),alpha=0.3)+
  theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "none",legend.title=element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=20),legend.text = element_blank())+ylim(-0.25,0.5)+geom_hline(yintercept = 0)+
  annotate("label", x=2017.5, y=0.45, label = wrapper(paste0(as.character(round_any(pretrendmat[24,1],0.001)),"  p=",as.character(round_any(pretrendmat[24,2],0.001)),collapse=NULL),width=5), size = 8)
ggsave("figure2d.pdf")

# Figure 2(e)
ggplot(pretrendmat[c(25:29),], aes(year, pretrendmatb))+
  geom_point()+
  geom_line(data=pretrendmat[c(25:29),],aes(color = label))+
  geom_ribbon(data=pretrendmat[c(25:29),],aes(ymin=pretrendmatb-pretrendmatse*1.96,ymax=pretrendmatb+pretrendmatse*1.96,fill=label),alpha=0.3)+
  theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "none",legend.title=element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=20),legend.text = element_blank())+ylim(-0.25,0.5)+geom_hline(yintercept = 0)+
  annotate("label", x=2017.5, y=0.45, label = wrapper(paste0(as.character(round_any(pretrendmat[30,1],0.001)),"  p=",as.character(round_any(pretrendmat[30,2],0.001)),collapse=NULL),width=5), size = 8)
ggsave("figure2e.pdf")

# Figures 3 and A.1
fig3_19<-read.csv("fig3_19.csv",stringsAsFactors=F,header=T)
fig3_20<-read.csv("fig3_20.csv",stringsAsFactors=F,header=T)
names(fig3_19)<-c("coef","se")
names(fig3_20)<-c("coef","se")
fig3_19$se<-1.96*fig3_19$se
fig3_20$se<-1.96*fig3_20$se

fig3<-rbind(fig3_19[c(1,7,6,5,2),],fig3_20[c(1,7,6,5,2),])
fig3$horizon<-c("2019","2019","2019","2019","2019","2020","2020","2020","2020","2020")
fig3$Outcome<-c("Contributions","Government Grants","Employment","Salaries","Program Spending","Contributions","Government Grants","Employment","Salaries","Program Spending")
fig3$val<-c(1:5,1:5)
fig3$Outcome <- factor(fig3$Outcome, levels = fig3$Outcome[order(fig3$val[c(1:5)])])
ggplot(fig3, aes(x = Outcome, y = coef, ymin = coef-se, ymax = coef+se,group=horizon))+geom_errorbar(aes(color=horizon),width=0.2,position=position_dodge(.4))+scale_x_discrete(breaks=c("Contributions","Government Grants","Salaries","Employment","Program Spending"),labels = function(x) lapply(strwrap(x, width = 10, simplify = FALSE), paste, collapse="\n")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),legend.text=element_text(size=10),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18))+geom_point(aes(color=horizon),size=2,position=position_dodge(.4))+ylim(-0.25,1)+geom_hline(yintercept = 0)+ scale_y_continuous(breaks=c(-0.25,-.5,0,.25,0.5,0.75,1))

ggsave("figure3.pdf")

figa1 <- rbind(fig3_19[c(3,4),],fig3_20[c(3,4),])
figa1$horizon<-c("2019","2019","2020","2020")
figa1$Outcome<-c("Fundraising","Revenue","Fundraising","Revenue")
figa1$val<-c(1:2,1:2)
figa1$Outcome <- factor(figa1$Outcome, levels = figa1$Outcome[order(figa1$val[c(1:2)])])

ggplot(figa1, aes(x = Outcome, y = coef, ymin = coef-se, ymax = coef+se,group=horizon))+geom_errorbar(aes(color=horizon),width=0.2,position=position_dodge(.4))+scale_x_discrete(breaks=c("Fundraising","Revenue"),labels = function(x) lapply(strwrap(x, width = 10, simplify = FALSE), paste, collapse="\n")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),legend.text=element_text(size=10),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18))+geom_point(aes(color=horizon),size=2,position=position_dodge(.4))+ylim(-0.15,0.15)+geom_hline(yintercept = 0)
ggsave("figurea1.pdf")

# Figure 4
att_filingmonth19<-read.csv("att_filingmonth19.csv")

att_filingmonth20<-read.csv("att_filingmonth20.csv")

f19<-as.data.frame(matrix(rep(NA,189),nrow=63,ncol=3))
names(f19)<-c("coef","se","Month")
f19$Month<-c(rep(3,7),rep(4,7),rep(5,7),rep(6,7),rep(7,7),rep(8,7),rep(9,7),rep(10,7), rep(11,7))
f19$coef<-att_filingmonth19$att_filingmonth19b
f19$se<-att_filingmonth19$att_filingmonth19se*1.96

f19$Var<-NA
f19$Var[c(1,8,15,22,29,36,43,50,57)]<-"Contributions, 2019"
f19$Var[c(2,9,16,23,30,37,44,51,58)]<-"Program Expenses, 2019"
f19$Var[c(3,10,17,24,31,38,45,52,59)]<-"Fundraising, 2019"
f19$Var[c(4,11,18,25,32,39,46,53,60)]<-"Revenue, 2019"
f19$Var[c(5,12,19,26,33,40,47,54,61)]<-"Salaries, 2019"
f19$Var[c(6,13,20,27,34,41,48,55,62)]<-"Employment, 2019"
f19$Var[c(7,14,21,28,35,42,49,56,63)]<-"Government Grants, 2019"

f20<-as.data.frame(matrix(rep(NA,189),nrow=63,ncol=3))
names(f20)<-c("coef","se","Month")
f20$Month<-c(rep(3,7),rep(4,7),rep(5,7),rep(6,7),rep(7,7),rep(8,7),rep(9,7),rep(10,7), rep(11,7))
f20$coef<-att_filingmonth20$att_filingmonth20b
f20$se<-att_filingmonth20$att_filingmonth20se*1.96

f20$Var<-NA
f20$Var[c(1,8,15,22,29,36,43,50,57)]<-"Contributions, 2020"
f20$Var[c(2,9,16,23,30,37,44,51,58)]<-"Program Expenses, 2020"
f20$Var[c(3,10,17,24,31,38,45,52,59)]<-"Fundraising, 2020"
f20$Var[c(4,11,18,25,32,39,46,53,60)]<-"Revenue, 2020"
f20$Var[c(5,12,19,26,33,40,47,54,61)]<-"Salaries, 2020"
f20$Var[c(6,13,20,27,34,41,48,55,62)]<-"Employment, 2020"
f20$Var[c(7,14,21,28,35,42,49,56,63)]<-"Government Grants, 2020"

# contrib and ggr
fcg19<-f19[c(1,7,8,14,15,21,22,28,29,35,36,42,43,49,50,56,57,63),]
fcg20<-f20[c(1,7,8,14,15,21,22,28,29,35,36,42,43,49,50,56,57,63),]
fcg<-rbind(fcg19,fcg20)

fempsal19<-f19[c(5,6,12,13,19,20,26,27,33,34,40,41,47,48,54,55,61,62),]
fempsal20<-f20[c(5,6,12,13,19,20,26,27,33,34,40,41,47,48,54,55,61,62),]
fempsal<-rbind(fempsal19,fempsal20)

fpse19<-f19[c(2,9,16,23,30,37,44,51,58),]
fpse20<-f20[c(2,9,16,23,30,37,44,51,58),]
fpse<-rbind(fpse19,fpse20)

ffund19<-f19[c(3,10,17,24,31,38,45,52,59),]
ffund20<-f20[c(3,10,17,24,31,38,45,52,59),]
ffund<-rbind(ffund19,ffund20)

ftrc19<-f19[c(4,11,18,25,32,39,46,53,60),]
ftrc20<-f20[c(4,11,18,25,32,39,46,53,60),]
ftrc<-rbind(ftrc19,ftrc20)

# Figure 4(a)
cgbar <- ggplot(data=fcg, aes(x=Month, y=coef, ymin = coef-se, ymax = coef+se,group=Var, fill=Var)) +  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4)) +geom_errorbar(width=0.2,position=position_dodge(0.4))+scale_x_continuous(breaks=c(3:11),labels=c("Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-1.25,2.5)+geom_hline(yintercept = 0) + scale_color_viridis(discrete=TRUE)
cgbar
ggsave("figure4a.pdf")

# Figure 4(b)
empsalbar <- ggplot(data=fempsal, aes(x=Month, y=coef, ymin = coef-se, ymax = coef+se,group=Var, fill=Var)) +  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4)) +geom_errorbar(width=0.2,position=position_dodge(0.4))+scale_x_continuous(breaks=c(3:11),labels=c("Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-0.5,0.05)+geom_hline(yintercept = 0) + scale_color_viridis(discrete=TRUE)
empsalbar
ggsave("figure4b.pdf")

# Figure 4(c)
psebar<- ggplot(data=fpse, aes(x=Month, y=coef, ymin = coef-se, ymax = coef+se,group=Var, fill=Var)) +  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4)) +geom_errorbar(width=0.2,position=position_dodge(0.4))+scale_x_continuous(breaks=c(3:11),labels=c("Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-0.5,0.5)+geom_hline(yintercept = 0) + scale_color_viridis(discrete=TRUE)
psebar
ggsave("figure4c.pdf")

# Figure 5
output19<-read.csv("forgraphsbycategory19.csv",header=TRUE)

anyout19<-as.data.frame(cbind(output19[,1],output19[,1]-1.96*output19[,2],output19[,1]+1.96*output19[,2]))
names(anyout19)<-c("coef","min","max")
anyout19$catlabel<-c("Contributions, 2019","Contributions, 2019","Contributions, 2019","Contributions, 2019","Contributions, 2019","Program Expenses, 2019","Program Expenses, 2019","Program Expenses, 2019","Program Expenses, 2019","Program Expenses, 2019","Fundraising, 2019","Fundraising, 2019","Fundraising, 2019","Fundraising, 2019","Fundraising, 2019","Revenue, 2019","Revenue, 2019","Revenue, 2019","Revenue, 2019","Revenue, 2019","Salaries, 2019","Salaries, 2019","Salaries, 2019","Salaries, 2019","Salaries, 2019","Employment, 2019","Employment, 2019","Employment, 2019","Employment, 2019","Employment, 2019","Government Grants, 2019","Government Grants, 2019","Government Grants, 2019","Government Grants, 2019","Government Grants, 2019")
anyout19$catlabel2<-c("Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other")
anyout19$category<-c(1,8,15,22,29,2,9,16,23,30,3,10,17,24,31,4,11,18,25,32,5,12,19,26,33,6,13,20,27,34,7,14,21,28,35)

output20<-read.csv("forgraphsbycategory20.csv",header=TRUE)

anyout20<-as.data.frame(cbind(output20[,1],output20[,1]-1.96*output20[,2],output20[,1]+1.96*output20[,2]))
names(anyout20)<-c("coef","min","max")
anyout20$catlabel<-c("Contributions, 2020","Contributions, 2020","Contributions, 2020","Contributions, 2020","Contributions, 2020","Program Expenses, 2020","Program Expenses, 2020","Program Expenses, 2020","Program Expenses, 2020","Program Expenses, 2020","Fundraising, 2020","Fundraising, 2020","Fundraising, 2020","Fundraising, 2020","Fundraising, 2020","Revenue, 2020","Revenue, 2020","Revenue, 2020","Revenue, 2020","Revenue, 2020","Salaries, 2020","Salaries, 2020","Salaries, 2020","Salaries, 2020","Salaries, 2020","Employment, 2020","Employment, 2020","Employment, 2020","Employment, 2020","Employment, 2020","Government Grants, 2020","Government Grants, 2020","Government Grants, 2020","Government Grants, 2020","Government Grants, 2020")
anyout20$catlabel2<-c("Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other")
anyout20$category<-c(1,8,15,22,29,2,9,16,23,30,3,10,17,24,31,4,11,18,25,32,5,12,19,26,33,6,13,20,27,34,7,14,21,28,35)

anyout19$cl<-factor(anyout19$catlabel,levels=c("Contributions, 2019","Program Expenses, 2019","Fundraising, 2019","Revenue, 2019","Salaries, 2019","Employment, 2019", "Government Grants, 2019"))
anyout19$cl2<-factor(anyout19$catlabel2,levels=c("Arts","Education","Health","Human Services","Other"))
anyout19<-anyout19[order(anyout19$category),]

anyout20$cl<-factor(anyout20$catlabel,levels=c("Contributions, 2020","Program Expenses, 2020","Fundraising, 2020","Revenue, 2020","Salaries, 2020","Employment, 2020", "Government Grants, 2020"))
anyout20$cl2<-factor(anyout20$catlabel2,levels=c("Arts","Education","Health","Human Services","Other"))
anyout20<-anyout20[order(anyout20$category),]

anyout<-rbind(anyout19,anyout20)

# Figure 5(a)
anycg<-anyout[which(anyout$cl == "Contributions, 2019" | anyout$cl == "Contributions, 2020" | anyout$cl == "Government Grants, 2019" | anyout$cl == "Government Grants, 2020"),]
anycg$cl<-factor(anycg$catlabel, levels = c("Contributions, 2019", "Contributions, 2020", "Government Grants, 2019", "Government Grants, 2020"))
anycg<-anycg[order(anycg$category),]

cg_bh <- ggplot(anycg, aes(x = cl2, y = coef, ymin = min, ymax = max,group=cl, fill=cl))+  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4))+geom_errorbar(width=0.2,position=position_dodge(.4))+scale_x_discrete(labels=c("Arts","Education","Health","Human Services","Other")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-0.25,2.25)+geom_hline(yintercept = 0)+ scale_color_viridis(discrete=TRUE)
cg_bh
ggsave("figure5a.pdf")


# Figure 5(b)
anyempsal<-anyout[which(anyout$cl == "Salaries, 2019" | anyout$cl == "Employment, 2019" |anyout$cl == "Salaries, 2020" | anyout$cl == "Employment, 2020"),]
anyempsal$cl<-factor(anyempsal$catlabel, levels = c("Employment, 2019", "Employment, 2020", "Salaries, 2019", "Salaries, 2020"))
anyempsal<-anyempsal[order(anyempsal$category),]

empsal_bh <- ggplot(anyempsal, aes(x = cl2, y = coef, ymin = min, ymax = max,group=cl, fill=cl))+  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4))+geom_errorbar(width=0.2,position=position_dodge(.4))+scale_x_discrete(labels=c("Arts","Education","Health","Human Services","Other")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-0.35,0.05)+geom_hline(yintercept = 0)+ scale_color_viridis(discrete=TRUE)
empsal_bh
ggsave("figure5b.pdf")

# Figure 5(c)
anypse<-anyout[which(anyout$cl == "Program Expenses, 2019" |anyout$cl == "Program Expenses, 2020"),]
anypse$cl<-factor(anypse$catlabel, levels = c("Program Expenses, 2019", "Program Expenses, 2020"))
anypse<-anypse[order(anypse$category),]

pse_bh <- ggplot(anypse, aes(x = cl2, y = coef, ymin = min, ymax = max,group=cl, fill=cl))+  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4))+geom_errorbar(width=0.2,position=position_dodge(.4))+scale_x_discrete(labels=c("Arts","Education","Health","Human Services","Other")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-0.25,0.1)+geom_hline(yintercept = 0)+ scale_color_viridis(discrete=TRUE)
pse_bh
ggsave("figure5c.pdf")

# Figure 6
fig6<-read.csv("fig6.csv",stringsAsFactors=F,header=T)
names(fig6)<-c("coef","se")
fig6$se<-1.96*fig6$se

fig6<-fig6[c(1,7,6,5,2),]
fig6$horizon<-c("2021","2021","2021","2021","2021")
fig6$Outcome<-c("Contributions","Government Grants","Employment","Salaries","Program Spending")
fig6$val<-c(1:5)
fig6$Outcome <- factor(fig6$Outcome, levels = fig6$Outcome[order(fig6$val[c(1:5)])])

ggplot(fig6, aes(x = Outcome, y = coef, ymin = coef-se, ymax = coef+se))+geom_errorbar(width=0.2,position=position_dodge(.4))+scale_x_discrete(breaks=c("Contributions","Government Grants","Salaries","Employment","Program Spending"),labels = function(x) lapply(strwrap(x, width = 10, simplify = FALSE), paste, collapse="\n")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),legend.text=element_text(size=10),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18))+geom_point(size=2,position=position_dodge(.4))+ylim(-0.5,0.1)+geom_hline(yintercept = 0)
ggsave("figure6.pdf")

# Figure 7

output21<-read.csv("forgraphsbycategory21.csv",header=TRUE)

anyout21<-as.data.frame(cbind(output21[,1],output21[,1]-1.96*output21[,2],output21[,1]+1.96*output21[,2]))
names(anyout21)<-c("coef","min","max")
anyout21$catlabel<-c("Contributions, 2021","Contributions, 2021","Contributions, 2021","Contributions, 2021","Contributions, 2021","Program Expenses, 2021","Program Expenses, 2021","Program Expenses, 2021","Program Expenses, 2021","Program Expenses, 2021","Fundraising, 2021","Fundraising, 2021","Fundraising, 2021","Fundraising, 2021","Fundraising, 2021","Revenue, 2021","Revenue, 2021","Revenue, 2021","Revenue, 2021","Revenue, 2021","Salaries, 2021","Salaries, 2021","Salaries, 2021","Salaries, 2021","Salaries, 2021","Employment, 2021","Employment, 2021","Employment, 2021","Employment, 2021","Employment, 2021","Government Grants, 2021","Government Grants, 2021","Government Grants, 2021","Government Grants, 2021","Government Grants, 2021")
anyout21$catlabel2<-c("Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other")
anyout21$category<-c(1,8,15,22,29,2,9,16,23,30,3,10,17,24,31,4,11,18,25,32,5,12,19,26,33,6,13,20,27,34,7,14,21,28,35)

anyout21$cl<-factor(anyout21$catlabel,levels=c("Contributions, 2021","Program Expenses, 2021","Fundraising, 2021","Revenue, 2021","Salaries, 2021","Employment, 2021", "Government Grants, 2021"))
anyout21$cl2<-factor(anyout21$catlabel2,levels=c("Arts","Education","Health","Human Services","Other"))
anyout21<-anyout21[order(anyout21$category),]

anycg21<-anyout21[which(anyout21$cl == "Contributions, 2021" | anyout21$cl == "Government Grants, 2021"),]
anycg21$cl<-factor(anycg21$catlabel, levels = c("Contributions, 2021", "Government Grants, 2021"))
anycg21<-anycg21[order(anycg21$category),]

# Figure 7(a)
cg_bh21 <- ggplot(anycg21, aes(x = cl2, y = coef, ymin = min, ymax = max,group=cl, fill=cl))+  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4))+geom_errorbar(width=0.2,position=position_dodge(.4))+scale_x_discrete(labels=c("Arts","Education","Health","Human Services","Other")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-1,1)+geom_hline(yintercept = 0)+ scale_color_viridis(discrete=TRUE)
cg_bh21
ggsave("figure7a.pdf")

anyempsal21<-anyout21[which(anyout21$cl == "Salaries, 2021" | anyout21$cl == "Employment, 2021"),]
anyempsal21$cl<-factor(anyempsal21$catlabel, levels = c("Employment, 2021", "Salaries, 2021"))
anyempsal21<-anyempsal21[order(anyempsal21$category),]

# Figure 7(b)
empsal_bh21 <- ggplot(anyempsal21, aes(x = cl2, y = coef, ymin = min, ymax = max,group=cl, fill=cl))+  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4))+geom_errorbar(width=0.2,position=position_dodge(.4))+scale_x_discrete(labels=c("Arts","Education","Health","Human Services","Other")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-0.4,0.1)+geom_hline(yintercept = 0)+ scale_color_viridis(discrete=TRUE)
empsal_bh21
ggsave("figure7b.pdf")


anypse21<-anyout21[which(anyout21$cl == "Program Expenses, 2021"),]
anypse21$cl<-factor(anypse21$catlabel, levels = c("Program Expenses, 2021"))
anypse21<-anypse21[order(anypse21$category),]

# Figure 7(c)
pse_bh21 <- ggplot(anypse21, aes(x = cl2, y = coef, ymin = min, ymax = max,group=cl, fill=cl))+  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4))+geom_errorbar(width=0.2,position=position_dodge(.4))+scale_x_discrete(labels=c("Arts","Education","Health","Human Services","Other")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-0.25,0.25)+geom_hline(yintercept = 0)+ scale_color_viridis(discrete=TRUE)
pse_bh21
ggsave("figure7c.pdf")

# Figure 8
att_filingmonth21<-read.csv("att_filingmonth21.csv")

f21<-as.data.frame(matrix(rep(NA,189),nrow=63,ncol=3))
names(f21)<-c("coef","se","Month")
f21$Month<-c(rep(3,7),rep(4,7),rep(5,7),rep(6,7),rep(7,7),rep(8,7),rep(9,7),rep(10,7), rep(11,7))
f21$coef<-att_filingmonth21$att_filingmonth21b
f21$se<-att_filingmonth21$att_filingmonth21se*1.96

f21$Var<-NA
f21$Var[c(1,8,15,22,29,36,43,50,57)]<-"Contributions, 2021"
f21$Var[c(2,9,16,23,30,37,44,51,58)]<-"Program Expenses, 2021"
f21$Var[c(3,10,17,24,31,38,45,52,59)]<-"Fundraising, 2021"
f21$Var[c(4,11,18,25,32,39,46,53,60)]<-"Revenue, 2021"
f21$Var[c(5,12,19,26,33,40,47,54,61)]<-"Salaries, 2021"
f21$Var[c(6,13,20,27,34,41,48,55,62)]<-"Employment, 2021"
f21$Var[c(7,14,21,28,35,42,49,56,63)]<-"Government Grants, 2021"

fcg21<-f21[c(1,7,8,14,15,21,22,28,29,35,36,42,43,49,50,56,57,63),]

fempsal21<-f21[c(5,6,12,13,19,20,26,27,33,34,40,41,47,48,54,55,61,62),]

fpse21<-f21[c(2,9,16,23,30,37,44,51,58),]

ffund21<-f21[c(3,10,17,24,31,38,45,52,59),]

ftrc21<-f21[c(4,11,18,25,32,39,46,53,60),]

# Figure 8(a)
cgbar21 <- ggplot(data=fcg21, aes(x=Month, y=coef, ymin = coef-se, ymax = coef+se,group=Var, fill=Var)) +  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4)) +geom_errorbar(width=0.2,position=position_dodge(0.4))+scale_x_continuous(breaks=c(3:11),labels=c("Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-2.5,1)+geom_hline(yintercept = 0) + scale_color_viridis(discrete=TRUE)
cgbar21
ggsave("figure8a.pdf")

# Figure 8(b)
empsalbar21 <- ggplot(data=fempsal21, aes(x=Month, y=coef, ymin = coef-se, ymax = coef+se,group=Var, fill=Var)) +  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4)) +geom_errorbar(width=0.2,position=position_dodge(0.4))+scale_x_continuous(breaks=c(3:11),labels=c("Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-1.25,0.5)+geom_hline(yintercept = 0) + scale_color_viridis(discrete=TRUE)
empsalbar21
ggsave("figure8b.pdf")

# Figure 8(c)
psebar21<- ggplot(data=fpse21, aes(x=Month, y=coef, ymin = coef-se, ymax = coef+se,group=Var, fill=Var)) +  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4)) +geom_errorbar(width=0.2,position=position_dodge(0.4))+scale_x_continuous(breaks=c(3:11),labels=c("Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-1,0.5)+geom_hline(yintercept = 0) + scale_color_viridis(discrete=TRUE)
psebar21
ggsave("figure8c.pdf")

# Figure B.1
fipsfeet<-read_dta("FootTraffic-Clean.dta")
fipsfeet<-unique(fipsfeet[which(fipsfeet$filingmonth=="06"),])
fipsfeet<-fipsfeet[,c(6,1)]
fipsfeet<-fipsfeet[order(fipsfeet$fips),]

att_contribcur_feet<-read.csv("att_contribcur_feet.csv")
att_contribcur_feet<-cbind(att_contribcur_feet,fipsfeet$cum_retail_rec)
names(att_contribcur_feet)[3]<-"cum_retail_rec"
att_contribcur_feet$state<-c("AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY")

att_contribcur_feet<-att_contribcur_feet[order(-att_contribcur_feet$cum_retail_rec),]
names(att_contribcur_feet)[1]<-"coef"
att_contribcur_feet$se<-att_contribcur_feet$att_contribcur_feetse*1.96
att_contribcur_feet$theorder<-c(1:51)
plot_att_contribcur_feet<-ggplot(att_contribcur_feet, aes(y = theorder, x = coef, xmin = coef-se, xmax = coef+se, label= state))+geom_errorbarh()+theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "none",panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"))+geom_point(aes(group=1),size=2, shape=18)+scale_y_discrete(limits=seq(from=1,to=51,by=1),labels=att_contribcur_feet$state)+geom_vline(xintercept = 0)

att_pse_feet<-read.csv("att_pse_feet.csv")
att_pse_feet<-cbind(att_pse_feet,fipsfeet$cum_retail_rec)
names(att_pse_feet)[3]<-"cum_retail_rec"
att_pse_feet$state<-c("AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY")

att_pse_feet<-att_pse_feet[order(-att_pse_feet$cum_retail_rec),]
names(att_pse_feet)[1]<-"coef"
att_pse_feet$se<-att_pse_feet$att_pse_feetse*1.96
att_pse_feet$theorder<-c(1:51)
plot_att_pse_feet<-ggplot(att_pse_feet, aes(y = theorder, x = coef, xmin = coef-se, xmax = coef+se, label= state))+geom_errorbarh()+theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "none",panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"))+geom_point(aes(group=1),size=2, shape=18)+scale_y_discrete(limits=seq(from=1,to=51,by=1),labels=att_pse_feet$state)+geom_vline(xintercept = 0)

att_salcur_feet<-read.csv("att_salcur_feet.csv")
att_salcur_feet<-cbind(att_salcur_feet,fipsfeet$cum_retail_rec)
names(att_salcur_feet)[3]<-"cum_retail_rec"
att_salcur_feet$state<-c("AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY")

att_salcur_feet<-att_salcur_feet[order(-att_salcur_feet$cum_retail_rec),]
names(att_salcur_feet)[1]<-"coef"
att_salcur_feet$se<-att_salcur_feet$att_salcur_feetse*1.96
att_salcur_feet$theorder<-c(1:51)
plot_att_salcur_feet<-ggplot(att_salcur_feet, aes(y = theorder, x = coef, xmin = coef-se, xmax = coef+se, label= state))+geom_errorbarh()+theme(axis.title.x=element_blank(),axis.title.y=element_blank(),panel.grid.minor = element_blank(),legend.position = "none",panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"))+geom_point(aes(group=1),size=2, shape=18)+scale_y_discrete(limits=seq(from=1,to=51,by=1),labels=att_salcur_feet$state)+geom_vline(xintercept = 0)

att_totemp_feet<-read.csv("att_totemp_feet.csv")
att_totemp_feet<-cbind(att_totemp_feet,fipsfeet$cum_retail_rec)
names(att_totemp_feet)[3]<-"cum_retail_rec"
att_totemp_feet$state<-c("AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY")

att_totemp_feet<-att_totemp_feet[order(-att_totemp_feet$cum_retail_rec),]
names(att_totemp_feet)[1]<-"coef"
att_totemp_feet$se<-att_totemp_feet$att_totemp_feetse*1.96
att_totemp_feet$theorder<-c(1:51)
plot_att_totemp_feet<-ggplot(att_totemp_feet, aes(y = theorder, x = coef, xmin = coef-se, xmax = coef+se, label= state))+geom_errorbarh()+theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "none",panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"))+geom_point(aes(group=1),size=2, shape=18)+scale_y_discrete(limits=seq(from=1,to=51,by=1),labels=att_totemp_feet$state)+geom_vline(xintercept = 0)

att_ggr_feet<-read.csv("att_ggr_feet.csv")
att_ggr_feet<-cbind(att_ggr_feet,fipsfeet$cum_retail_rec)
names(att_ggr_feet)[3]<-"cum_retail_rec"
att_ggr_feet$state<-c("AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY")

att_ggr_feet<-att_ggr_feet[order(-att_ggr_feet$cum_retail_rec),]
names(att_ggr_feet)[1]<-"coef"
att_ggr_feet$se<-att_ggr_feet$att_ggr_feetse*1.96
att_ggr_feet$theorder<-c(1:51)
plot_att_ggr_feet<-ggplot(att_ggr_feet, aes(y = theorder, x = coef, xmin = coef-se, xmax = coef+se, label= state))+geom_errorbarh()+theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "none",panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"))+geom_point(aes(group=1),size=2, shape=18)+scale_y_discrete(limits=seq(from=1,to=51,by=1),labels=att_ggr_feet$state)+geom_vline(xintercept = 0)

# Figure B.1(a)
plot_att_contribcur_feet
ggsave("figureb1a.pdf")
# Figure B.1(b)
plot_att_ggr_feet
ggsave("figureb1b.pdf")
# Figure B.1(c)
plot_att_totemp_feet
ggsave("figureb1c.pdf")
# Figure B.1(d)
plot_att_salcur_feet
ggsave("figureb1d.pdf")
# Figure B.1(e)
plot_att_pse_feet
ggsave("figureb1e.pdf")

# Figure C.1
figc1_19 <-read.csv("figc1_19.csv",stringsAsFactors = F,header=T)
figc1_20 <-read.csv("figc1_20.csv",stringsAsFactors = F,header=T)
names(figc1_19)<-c("coef","se")
names(figc1_20)<-c("coef","se")
figc1_19$se<-1.96*figc1_19$se
figc1_20$se<-1.96*figc1_20$se

figc1<-rbind(figc1_19[c(1,7,6,5,2),],figc1_20[c(1,7,6,5,2),])
figc1$horizon<-c("2019","2019","2019","2019","2019","2020","2020","2020","2020","2020")
figc1$Outcome<-c("Contributions","Government Grants","Employment","Salaries","Program Spending","Contributions","Government Grants","Employment","Salaries","Program Spending")
figc1$val<-c(1:5,1:5)
figc1$Outcome <- factor(figc1$Outcome, levels = figc1$Outcome[order(figc1$val[c(1:5)])])
ggplot(figc1, aes(x = Outcome, y = coef, ymin = coef-se, ymax = coef+se,group=horizon))+geom_errorbar(aes(color=horizon),width=0.2,position=position_dodge(.4))+scale_x_discrete(breaks=c("Contributions","Government Grants","Salaries","Employment","Program Spending"),labels = function(x) lapply(strwrap(x, width = 10, simplify = FALSE), paste, collapse="\n")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),legend.text=element_text(size=10),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18))+geom_point(aes(color=horizon),size=2,position=position_dodge(.4))+ylim(-0.25,1)+geom_hline(yintercept = 0)+ scale_y_continuous(breaks=c(-0.25,-.5,0,.25,0.5,0.75,1))

ggsave("figurec1.pdf")

# Figure C.2
att_filingmonth19<-read.csv("att_filingmonth_noppp19.csv")
names(att_filingmonth19)<-c("att_filingmonth19b","att_filingmonth19se")
att_filingmonth20<-read.csv("att_filingmonth_noppp20.csv")
names(att_filingmonth20)<-c("att_filingmonth20b","att_filingmonth20se")

f19<-as.data.frame(matrix(rep(NA,189),nrow=63,ncol=3))
names(f19)<-c("coef","se","Month")
f19$Month<-c(rep(3,7),rep(4,7),rep(5,7),rep(6,7),rep(7,7),rep(8,7),rep(9,7),rep(10,7), rep(11,7))
f19$coef<-att_filingmonth19$att_filingmonth19b
f19$se<-att_filingmonth19$att_filingmonth19se*1.96

f19$Var<-NA
f19$Var[c(1,8,15,22,29,36,43,50,57)]<-"Contributions, 2019"
f19$Var[c(2,9,16,23,30,37,44,51,58)]<-"Program Expenses, 2019"
f19$Var[c(3,10,17,24,31,38,45,52,59)]<-"Fundraising, 2019"
f19$Var[c(4,11,18,25,32,39,46,53,60)]<-"Revenue, 2019"
f19$Var[c(5,12,19,26,33,40,47,54,61)]<-"Salaries, 2019"
f19$Var[c(6,13,20,27,34,41,48,55,62)]<-"Employment, 2019"
f19$Var[c(7,14,21,28,35,42,49,56,63)]<-"Government Grants, 2019"

f20<-as.data.frame(matrix(rep(NA,189),nrow=63,ncol=3))
names(f20)<-c("coef","se","Month")
f20$Month<-c(rep(3,7),rep(4,7),rep(5,7),rep(6,7),rep(7,7),rep(8,7),rep(9,7),rep(10,7), rep(11,7))
f20$coef<-att_filingmonth20$att_filingmonth20b
f20$se<-att_filingmonth20$att_filingmonth20se*1.96

f20$Var<-NA
f20$Var[c(1,8,15,22,29,36,43,50,57)]<-"Contributions, 2020"
f20$Var[c(2,9,16,23,30,37,44,51,58)]<-"Program Expenses, 2020"
f20$Var[c(3,10,17,24,31,38,45,52,59)]<-"Fundraising, 2020"
f20$Var[c(4,11,18,25,32,39,46,53,60)]<-"Revenue, 2020"
f20$Var[c(5,12,19,26,33,40,47,54,61)]<-"Salaries, 2020"
f20$Var[c(6,13,20,27,34,41,48,55,62)]<-"Employment, 2020"
f20$Var[c(7,14,21,28,35,42,49,56,63)]<-"Government Grants, 2020"

# contrib and ggr
fcg19<-f19[c(1,7,8,14,15,21,22,28,29,35,36,42,43,49,50,56,57,63),]
fcg20<-f20[c(1,7,8,14,15,21,22,28,29,35,36,42,43,49,50,56,57,63),]
fcg<-rbind(fcg19,fcg20)

fempsal19<-f19[c(5,6,12,13,19,20,26,27,33,34,40,41,47,48,54,55,61,62),]
fempsal20<-f20[c(5,6,12,13,19,20,26,27,33,34,40,41,47,48,54,55,61,62),]
fempsal<-rbind(fempsal19,fempsal20)

fpse19<-f19[c(2,9,16,23,30,37,44,51,58),]
fpse20<-f20[c(2,9,16,23,30,37,44,51,58),]
fpse<-rbind(fpse19,fpse20)

ffund19<-f19[c(3,10,17,24,31,38,45,52,59),]
ffund20<-f20[c(3,10,17,24,31,38,45,52,59),]
ffund<-rbind(ffund19,ffund20)

ftrc19<-f19[c(4,11,18,25,32,39,46,53,60),]
ftrc20<-f20[c(4,11,18,25,32,39,46,53,60),]
ftrc<-rbind(ftrc19,ftrc20)

# Figure C.2(a)
cgbar <- ggplot(data=fcg, aes(x=Month, y=coef, ymin = coef-se, ymax = coef+se,group=Var, fill=Var)) +  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4)) +geom_errorbar(width=0.2,position=position_dodge(0.4))+scale_x_continuous(breaks=c(3:11),labels=c("Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-1.25,2.5)+geom_hline(yintercept = 0) + scale_color_viridis(discrete=TRUE)
dev.new(width=12,height=5,noRStudioGD = TRUE)
cgbar
ggsave("figurec2a.pdf")

# Figure C.2(b)
empsalbar <- ggplot(data=fempsal, aes(x=Month, y=coef, ymin = coef-se, ymax = coef+se,group=Var, fill=Var)) +  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4)) +geom_errorbar(width=0.2,position=position_dodge(0.4))+scale_x_continuous(breaks=c(3:11),labels=c("Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-0.4,0.1)+geom_hline(yintercept = 0) + scale_color_viridis(discrete=TRUE)
dev.new(width=12,height=5,noRStudioGD = TRUE)
empsalbar
ggsave("figurec2b.pdf")

# Figure C.2(c)
psebar<- ggplot(data=fpse, aes(x=Month, y=coef, ymin = coef-se, ymax = coef+se,group=Var, fill=Var)) +  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4)) +geom_errorbar(width=0.2,position=position_dodge(0.4))+scale_x_continuous(breaks=c(3:11),labels=c("Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-0.5,0.5)+geom_hline(yintercept = 0) + scale_color_viridis(discrete=TRUE)
dev.new(width=12,height=5,noRStudioGD = TRUE)
psebar
ggsave("figurec2c.pdf")

# Figure C.3

output19<-read.csv("forgraphsbycategory19_noppp.csv",header=TRUE)

anyout19<-as.data.frame(cbind(output19[,1],output19[,1]-1.96*output19[,2],output19[,1]+1.96*output19[,2]))
names(anyout19)<-c("coef","min","max")
anyout19$catlabel<-c("Contributions, 2019","Contributions, 2019","Contributions, 2019","Contributions, 2019","Contributions, 2019","Program Expenses, 2019","Program Expenses, 2019","Program Expenses, 2019","Program Expenses, 2019","Program Expenses, 2019","Fundraising, 2019","Fundraising, 2019","Fundraising, 2019","Fundraising, 2019","Fundraising, 2019","Revenue, 2019","Revenue, 2019","Revenue, 2019","Revenue, 2019","Revenue, 2019","Salaries, 2019","Salaries, 2019","Salaries, 2019","Salaries, 2019","Salaries, 2019","Employment, 2019","Employment, 2019","Employment, 2019","Employment, 2019","Employment, 2019","Government Grants, 2019","Government Grants, 2019","Government Grants, 2019","Government Grants, 2019","Government Grants, 2019")
anyout19$catlabel2<-c("Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other")
anyout19$category<-c(1,8,15,22,29,2,9,16,23,30,3,10,17,24,31,4,11,18,25,32,5,12,19,26,33,6,13,20,27,34,7,14,21,28,35)

output20<-read.csv("forgraphsbycategory20_noppp.csv",header=TRUE)

anyout20<-as.data.frame(cbind(output20[,1],output20[,1]-1.96*output20[,2],output20[,1]+1.96*output20[,2]))
names(anyout20)<-c("coef","min","max")
anyout20$catlabel<-c("Contributions, 2020","Contributions, 2020","Contributions, 2020","Contributions, 2020","Contributions, 2020","Program Expenses, 2020","Program Expenses, 2020","Program Expenses, 2020","Program Expenses, 2020","Program Expenses, 2020","Fundraising, 2020","Fundraising, 2020","Fundraising, 2020","Fundraising, 2020","Fundraising, 2020","Revenue, 2020","Revenue, 2020","Revenue, 2020","Revenue, 2020","Revenue, 2020","Salaries, 2020","Salaries, 2020","Salaries, 2020","Salaries, 2020","Salaries, 2020","Employment, 2020","Employment, 2020","Employment, 2020","Employment, 2020","Employment, 2020","Government Grants, 2020","Government Grants, 2020","Government Grants, 2020","Government Grants, 2020","Government Grants, 2020")
anyout20$catlabel2<-c("Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other","Arts","Education","Health","Human Services","Other")
anyout20$category<-c(1,8,15,22,29,2,9,16,23,30,3,10,17,24,31,4,11,18,25,32,5,12,19,26,33,6,13,20,27,34,7,14,21,28,35)

anyout19$cl<-factor(anyout19$catlabel,levels=c("Contributions, 2019","Program Expenses, 2019","Fundraising, 2019","Revenue, 2019","Salaries, 2019","Employment, 2019", "Government Grants, 2019"))
anyout19$cl2<-factor(anyout19$catlabel2,levels=c("Arts","Education","Health","Human Services","Other"))
anyout19<-anyout19[order(anyout19$category),]

anyout20$cl<-factor(anyout20$catlabel,levels=c("Contributions, 2020","Program Expenses, 2020","Fundraising, 2020","Revenue, 2020","Salaries, 2020","Employment, 2020", "Government Grants, 2020"))
anyout20$cl2<-factor(anyout20$catlabel2,levels=c("Arts","Education","Health","Human Services","Other"))
anyout20<-anyout20[order(anyout20$category),]

anyout<-rbind(anyout19,anyout20)

# Figure C.3(a)
anycg<-anyout[which(anyout$cl == "Contributions, 2019" | anyout$cl == "Contributions, 2020" | anyout$cl == "Government Grants, 2019" | anyout$cl == "Government Grants, 2020"),]
anycg$cl<-factor(anycg$catlabel, levels = c("Contributions, 2019", "Contributions, 2020", "Government Grants, 2019", "Government Grants, 2020"))
anycg<-anycg[order(anycg$category),]
cg_bh <- ggplot(anycg, aes(x = cl2, y = coef, ymin = min, ymax = max,group=cl, fill=cl))+  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4))+geom_errorbar(width=0.2,position=position_dodge(.4))+scale_x_discrete(labels=c("Arts","Education","Health","Human Services","Other")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-0.25,2.5)+geom_hline(yintercept = 0)+ scale_color_viridis(discrete=TRUE)
dev.new(width=12,height=5,noRStudioGD = TRUE)
cg_bh
ggsave("figurec3a.pdf")


# Figure C.3(b)
anyempsal<-anyout[which(anyout$cl == "Salaries, 2019" | anyout$cl == "Employment, 2019" |anyout$cl == "Salaries, 2020" | anyout$cl == "Employment, 2020"),]
anyempsal$cl<-factor(anyempsal$catlabel, levels = c("Employment, 2019", "Employment, 2020", "Salaries, 2019", "Salaries, 2020"))
anyempsal<-anyempsal[order(anyempsal$category),]
empsal_bh <- ggplot(anyempsal, aes(x = cl2, y = coef, ymin = min, ymax = max,group=cl, fill=cl))+  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4))+geom_errorbar(width=0.2,position=position_dodge(.4))+scale_x_discrete(labels=c("Arts","Education","Health","Human Services","Other")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-0.35,0.05)+geom_hline(yintercept = 0)+ scale_color_viridis(discrete=TRUE)
dev.new(width=12,height=5,noRStudioGD = TRUE)
empsal_bh
ggsave("figurec3b.pdf")

# Figure C.3(c)
anypse<-anyout[which(anyout$cl == "Program Expenses, 2019" |anyout$cl == "Program Expenses, 2020"),]
anypse$cl<-factor(anypse$catlabel, levels = c("Program Expenses, 2019", "Program Expenses, 2020"))
anypse<-anypse[order(anypse$category),]
pse_bh <- ggplot(anypse, aes(x = cl2, y = coef, ymin = min, ymax = max,group=cl, fill=cl))+  geom_bar(stat="identity",width = 0.5, position=position_dodge(0.4))+geom_errorbar(width=0.2,position=position_dodge(.4))+scale_x_discrete(labels=c("Arts","Education","Health","Human Services","Other")) +theme(axis.title.x=element_blank(),axis.title.y=element_blank(),legend.position = "bottom",legend.title = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "gray"),text=element_text(size=18), legend.text = element_text(size=14))+ylim(-0.3,0.2)+geom_hline(yintercept = 0)+ scale_color_viridis(discrete=TRUE)
dev.new(width=12,height=5,noRStudioGD = TRUE)
pse_bh
ggsave("figurec3c.pdf")

